package com.example.hxpc.guidancemanagement.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.example.hxpc.guidancemanagement.R;
import com.example.hxpc.guidancemanagement.activity.GoodProjectDetailActivity;
import com.example.hxpc.guidancemanagement.adapter.MyListViewAdapter;
import com.example.hxpc.guidancemanagement.api.BaseApi;
import com.example.hxpc.guidancemanagement.base.HonorInfo;
import com.example.hxpc.guidancemanagement.model.JsonBase;
import com.example.hxpc.guidancemanagement.model.JsonFeedback;
import com.example.hxpc.guidancemanagement.tools.ToastTools;
import com.example.hxpc.guidancemanagement.tools.dataTools.OkHttpTools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tao on 2017/11/29.
 * 主页面-荣誉版Frament
 */

public class MainHonFragment extends Fragment implements View.OnClickListener ,BaseApi,AdapterView.OnItemClickListener {
    private View rootView ,bg_view_1 , bg_view_2;
    private TextView te_good_project , te_good_people;
    private RelativeLayout rela_good_project , rela_good_people;
    private ListView listView;
    private OkHttpTools okHttpTools = new OkHttpTools();
    private List<HonorInfo> mList = new ArrayList<>();
    private ViewFlipper viewFlipper;
    private int[] images = new int[]{R.drawable.we,R.drawable.metting,R.drawable.reward};
    private long refreshTime = 0;//用来记录Fragment刷新的时间
    private int mark = 1;//判断当前处于哪个栏目的标志
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.frament_main_honor,null);
        }
        refreshTime = 0;
        initView(rootView);
        return rootView;
    }

    //添加图片
    public void addImgPhoto(){
        for (int i = 0 ;i <images.length ; i++){
            ImageView imageView = new ImageView(getActivity());
            imageView.setBackgroundResource(images[i]);
            viewFlipper.addView(imageView);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser && isVisible()){
            if (refreshTime == 0 || (refreshTime != 0 && (System.currentTimeMillis() - refreshTime)>30*60*1000)){
                if (mark == 1){
                    okHttpTools.getGoodProject(this);
                }else if (mark == 2){
                    okHttpTools.getGoodPerson(this);
                }
            }
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    //初始化控件
    public void initView(View rootView){
        rela_good_project = (RelativeLayout) rootView.findViewById(R.id.honor_rela_1);
        rela_good_project.setOnClickListener(this);
        rela_good_people = (RelativeLayout) rootView.findViewById(R.id.honor_rela_2);
        rela_good_people.setOnClickListener(this);
        te_good_project = (TextView) rootView.findViewById(R.id.text_good_project);
        te_good_people = (TextView) rootView.findViewById(R.id.text_good_people);
        bg_view_1 = rootView.findViewById(R.id.view_1);
        bg_view_2 = rootView.findViewById(R.id.view_2);
        listView = (ListView) rootView.findViewById(R.id.list);
        listView.setOnItemClickListener(this);
        viewFlipper = (ViewFlipper) rootView.findViewById(R.id.viewflipper);
        addImgPhoto();
        viewFlipper.setAutoStart(true);
        viewFlipper.setFlipInterval(3000);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDestroy() {
        te_good_project.setTextColor(this.getResources().getColor(R.color.blue));
        bg_view_1.setBackgroundResource(R.color.blue);
        te_good_people.setTextColor(this.getResources().getColor(R.color.gray));
        bg_view_2.setBackgroundResource(R.color.gray);
        super.onDestroy();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.honor_rela_1:
                mark = 1;
                te_good_project.setTextColor(this.getResources().getColor(R.color.blue));
                bg_view_1.setBackgroundResource(R.color.blue);
                te_good_people.setTextColor(this.getResources().getColor(R.color.gray));
                bg_view_2.setBackgroundResource(R.color.gray);
                okHttpTools.getGoodProject(this);
                break;
            case R.id.honor_rela_2:
                mark = 2;
                te_good_project.setTextColor(this.getResources().getColor(R.color.gray));
                bg_view_1.setBackgroundResource(R.color.gray);
                te_good_people.setTextColor(this.getResources().getColor(R.color.blue));
                bg_view_2.setBackgroundResource(R.color.blue);
                okHttpTools.getGoodPerson(this);
                break;
        }
    }

    @Override
    public void backMsg(String success, JsonBase jsonBase) {

    }

    @Override
    public void backMsg(String success, JsonFeedback jsonFeedback) {
        if (success.equals("true")&&jsonFeedback.getMsg().equals("获取优秀项目成功")){
            mList.clear();
            for (int i =0;i<jsonFeedback.getData().size();i++){
                HonorInfo info = new HonorInfo();
                info.setTitle(jsonFeedback.getData().get(i).getProjectName());
                info.setAvatar(jsonFeedback.getData().get(i).getPhotoPath());
                info.setDetails(jsonFeedback.getData().get(i).getContent());
                info.setResourcePath(jsonFeedback.getData().get(i).getResourcePath());
                mList.add(info);
            }
            listView.setAdapter(new MyListViewAdapter(getActivity(),mList ));
        }else if (success.equals("true")&&jsonFeedback.getMsg().equals("获取优秀人物成功")){
            mList.clear();
            for (int i =0;i<jsonFeedback.getData().size();i++){
                HonorInfo info = new HonorInfo();
                info.setTitle(jsonFeedback.getData().get(i).getPersonName());
                info.setAvatar(jsonFeedback.getData().get(i).getPersonPhoto());
                info.setDetails(jsonFeedback.getData().get(i).getPersonBrief());
                info.setResourcePath(jsonFeedback.getData().get(i).getPersonDetail());
                mList.add(info);
            }
            listView.setAdapter(new MyListViewAdapter(getActivity(),mList ));
        }else {
            ToastTools.toastMsg(getActivity(),jsonFeedback.getMsg());
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), GoodProjectDetailActivity.class);
        intent.putExtra("resourcePath",mList.get(position).getResourcePath());
        startActivity(intent);
    }
}
